package com.hexiang.error;

import com.hexiang.response.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;

import javax.servlet.http.HttpServletRequest;

/**
 * 统一错误码异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalErrorInfoHandler {

    @ExceptionHandler(value = Exception.class)
    public CommonResponse handleException(HttpServletRequest request, Exception exception) {
        log.error("WebAsyncTaskError,error is ", exception);
        return CommonResponse.builder().success(false).message("task error").build();
    }

    @ExceptionHandler(value = AsyncRequestTimeoutException.class)
    public CommonResponse handleAsyncRequestTimeoutException(HttpServletRequest request, AsyncRequestTimeoutException exception) {
        log.info("WebAsyncTaskTimedOut");
        return CommonResponse.builder().success(false).message("task timed out").build();
    }
}
